home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / Programming / gcc-2.95.3-3 / info / g77.info-15 < prev    next >
Encoding:
GNU Info File  |  2001-07-15  |  46.3 KB  |  1,045 lines

  1. This is Info file f/g77.info, produced by Makeinfo version 1.68 from
  2. the input file ./f/g77.texi.
  3.  
  4. INFO-DIR-SECTION Programming
  5. START-INFO-DIR-ENTRY
  6. * g77: (g77).                  The GNU Fortran compiler.
  7. END-INFO-DIR-ENTRY
  8.    This file documents the use and the internals of the GNU Fortran
  9. (`g77') compiler.  It corresponds to the GCC-2.95 version of `g77'.
  10.  
  11.    Published by the Free Software Foundation 59 Temple Place - Suite 330
  12. Boston, MA 02111-1307 USA
  13.  
  14.    Copyright (C) 1995-1999 Free Software Foundation, Inc.
  15.  
  16.    Permission is granted to make and distribute verbatim copies of this
  17. manual provided the copyright notice and this permission notice are
  18. preserved on all copies.
  19.  
  20.    Permission is granted to copy and distribute modified versions of
  21. this manual under the conditions for verbatim copying, provided also
  22. that the sections entitled "GNU General Public License," "Funding for
  23. Free Software," and "Protect Your Freedom--Fight `Look And Feel'" are
  24. included exactly as in the original, and provided that the entire
  25. resulting derived work is distributed under the terms of a permission
  26. notice identical to this one.
  27.  
  28.    Permission is granted to copy and distribute translations of this
  29. manual into another language, under the above conditions for modified
  30. versions, except that the sections entitled "GNU General Public
  31. License," "Funding for Free Software," and "Protect Your Freedom--Fight
  32. `Look And Feel'", and this permission notice, may be included in
  33. translations approved by the Free Software Foundation instead of in the
  34. original English.
  35.  
  36.    Contributed by James Craig Burley (<craig@jcb-sc.com>).  Inspired by
  37. a first pass at translating `g77-0.5.16/f/DOC' that was contributed to
  38. Craig by David Ronis (<ronis@onsager.chem.mcgill.ca>).
  39.  
  40. 
  41. File: g77.info,  Node: Surprising Interpretations of Code,  Next: Aliasing Assumed To Work,  Prev: Unused Arguments,  Up: Working Programs
  42.  
  43. Surprising Interpretations of Code
  44. ----------------------------------
  45.  
  46.    The `-Wsurprising' option can help find bugs involving expression
  47. evaluation or in the way `DO' loops with non-integral iteration
  48. variables are handled.  Cases found by this option might indicate a
  49. difference of interpretation between the author of the code involved,
  50. and a standard-conforming compiler such as `g77'.  Such a difference
  51. might produce actual bugs.
  52.  
  53.    In any case, changing the code to explicitly do what the programmer
  54. might have expected it to do, so `g77' and other compilers are more
  55. likely to follow the programmer's expectations, might be worthwhile,
  56. especially if such changes make the program work better.
  57.  
  58. 
  59. File: g77.info,  Node: Aliasing Assumed To Work,  Next: Output Assumed To Flush,  Prev: Surprising Interpretations of Code,  Up: Working Programs
  60.  
  61. Aliasing Assumed To Work
  62. ------------------------
  63.  
  64.    The `-falias-check', `-fargument-alias', `-fargument-noalias', and
  65. `-fno-argument-noalias-global' options, introduced in version 0.5.20 and
  66. `g77''s version 2.7.2.2.f.2 of `gcc', were withdrawn as of `g77'
  67. version 0.5.23 due to their not being supported by `gcc' version 2.8.
  68.  
  69.    These options, which control the assumptions regarding aliasing
  70. (overlapping) of writes and reads to main memory (core) made by the
  71. `gcc' back end, might well be added back (in some form) in a future
  72. version of `gcc'.
  73.  
  74.    However, these options *are* supported by `egcs'.
  75.  
  76.    The information below still is useful, but applies to only those
  77. versions of `g77' that support the alias analysis implied by support
  78. for these options.
  79.  
  80.    These options are effective only when compiling with `-O'
  81. (specifying any level other than `-O0') or with `-falias-check'.
  82.  
  83.    The default for Fortran code is `-fargument-noalias-global'.  (The
  84. default for C code and code written in other C-based languages is
  85. `-fargument-alias'.  These defaults apply regardless of whether you use
  86. `g77' or `gcc' to compile your code.)
  87.  
  88.    Note that, on some systems, compiling with `-fforce-addr' in effect
  89. can produce more optimal code when the default aliasing options are in
  90. effect (and when optimization is enabled).
  91.  
  92.    If your program is not working when compiled with optimization, it
  93. is possible it is violating the Fortran standards (77 and 90) by
  94. relying on the ability to "safely" modify variables and arrays that are
  95. aliased, via procedure calls, to other variables and arrays, without
  96. using `EQUIVALENCE' to explicitly set up this kind of aliasing.
  97.  
  98.    (The FORTRAN 77 standard's prohibition of this sort of overlap,
  99. generally referred to therein as "storage assocation", appears in
  100. Sections 15.9.3.6.  This prohibition allows implementations, such as
  101. `g77', to, for example, implement the passing of procedures and even
  102. values in `COMMON' via copy operations into local, perhaps more
  103. efficiently accessed temporaries at entry to a procedure, and, where
  104. appropriate, via copy operations back out to their original locations
  105. in memory at exit from that procedure, without having to take into
  106. consideration the order in which the local copies are updated by the
  107. code, among other things.)
  108.  
  109.    To test this hypothesis, try compiling your program with the
  110. `-fargument-alias' option, which causes the compiler to revert to
  111. assumptions essentially the same as made by versions of `g77' prior to
  112. 0.5.20.
  113.  
  114.    If the program works using this option, that strongly suggests that
  115. the bug is in your program.  Finding and fixing the bug(s) should
  116. result in a program that is more standard-conforming and that can be
  117. compiled by `g77' in a way that results in a faster executable.
  118.  
  119.    (You might want to try compiling with `-fargument-noalias', a kind
  120. of half-way point, to see if the problem is limited to aliasing between
  121. dummy arguments and `COMMON' variables--this option assumes that such
  122. aliasing is not done, while still allowing aliasing among dummy
  123. arguments.)
  124.  
  125.    An example of aliasing that is invalid according to the standards is
  126. shown in the following program, which might *not* produce the expected
  127. results when executed:
  128.  
  129.      I = 1
  130.      CALL FOO(I, I)
  131.      PRINT *, I
  132.      END
  133.      
  134.      SUBROUTINE FOO(J, K)
  135.      J = J + K
  136.      K = J * K
  137.      PRINT *, J, K
  138.      END
  139.  
  140.    The above program attempts to use the temporary aliasing of the `J'
  141. and `K' arguments in `FOO' to effect a pathological behavior--the
  142. simultaneous changing of the values of *both* `J' and `K' when either
  143. one of them is written.
  144.  
  145.    The programmer likely expects the program to print these values:
  146.  
  147.      2  4
  148.      4
  149.  
  150.    However, since the program is not standard-conforming, an
  151. implementation's behavior when running it is undefined, because
  152. subroutine `FOO' modifies at least one of the arguments, and they are
  153. aliased with each other.  (Even if one of the assignment statements was
  154. deleted, the program would still violate these rules.  This kind of
  155. on-the-fly aliasing is permitted by the standard only when none of the
  156. aliased items are defined, or written, while the aliasing is in effect.)
  157.  
  158.    As a practical example, an optimizing compiler might schedule the `J
  159. =' part of the second line of `FOO' *after* the reading of `J' and `K'
  160. for the `J * K' expression, resulting in the following output:
  161.  
  162.      2  2
  163.      2
  164.  
  165.    Essentially, compilers are promised (by the standard and, therefore,
  166. by programmers who write code they claim to be standard-conforming)
  167. that if they cannot detect aliasing via static analysis of a single
  168. program unit's `EQUIVALENCE' and `COMMON' statements, no such aliasing
  169. exists.  In such cases, compilers are free to assume that an assignment
  170. to one variable will not change the value of another variable, allowing
  171. it to avoid generating code to re-read the value of the other variable,
  172. to re-schedule reads and writes, and so on, to produce a faster
  173. executable.
  174.  
  175.    The same promise holds true for arrays (as seen by the called
  176. procedure)--an element of one dummy array cannot be aliased with, or
  177. overlap, any element of another dummy array or be in a `COMMON' area
  178. known to the procedure.
  179.  
  180.    (These restrictions apply only when the procedure defines, or writes
  181. to, one of the aliased variables or arrays.)
  182.  
  183.    Unfortunately, there is no way to find *all* possible cases of
  184. violations of the prohibitions against aliasing in Fortran code.
  185. Static analysis is certainly imperfect, as is run-time analysis, since
  186. neither can catch all violations.  (Static analysis can catch all
  187. likely violations, and some that might never actually happen, while
  188. run-time analysis can catch only those violations that actually happen
  189. during a particular run.  Neither approach can cope with programs
  190. mixing Fortran code with routines written in other languages, however.)
  191.  
  192.    Currently, `g77' provides neither static nor run-time facilities to
  193. detect any cases of this problem, although other products might.
  194. Run-time facilities are more likely to be offered by future versions of
  195. `g77', though patches improving `g77' so that it provides either form
  196. of detection are welcome.
  197.  
  198. 
  199. File: g77.info,  Node: Output Assumed To Flush,  Next: Large File Unit Numbers,  Prev: Aliasing Assumed To Work,  Up: Working Programs
  200.  
  201. Output Assumed To Flush
  202. -----------------------
  203.  
  204.    For several versions prior to 0.5.20, `g77' configured its version
  205. of the `libf2c' run-time library so that one of its configuration
  206. macros, `ALWAYS_FLUSH', was defined.
  207.  
  208.    This was done as a result of a belief that many programs expected
  209. output to be flushed to the operating system (under UNIX, via the
  210. `fflush()' library call) with the result that errors, such as disk
  211. full, would be immediately flagged via the relevant `ERR=' and
  212. `IOSTAT=' mechanism.
  213.  
  214.    Because of the adverse effects this approach had on the performance
  215. of many programs, `g77' no longer configures `libf2c' (now named
  216. `libg2c' in its `g77' incarnation) to always flush output.
  217.  
  218.    If your program depends on this behavior, either insert the
  219. appropriate `CALL FLUSH' statements, or modify the sources to the
  220. `libg2c', rebuild and reinstall `g77', and relink your programs with
  221. the modified library.
  222.  
  223.    (Ideally, `libg2c' would offer the choice at run-time, so that a
  224. compile-time option to `g77' or `f2c' could result in generating the
  225. appropriate calls to flushing or non-flushing library routines.)
  226.  
  227.    *Note Always Flush Output::, for information on how to modify the
  228. `g77' source tree so that a version of `libg2c' can be built and
  229. installed with the `ALWAYS_FLUSH' macro defined.
  230.  
  231. 
  232. File: g77.info,  Node: Large File Unit Numbers,  Next: Floating-point precision,  Prev: Output Assumed To Flush,  Up: Working Programs
  233.  
  234. Large File Unit Numbers
  235. -----------------------
  236.  
  237.    If your program crashes at run time with a message including the
  238. text `illegal unit number', that probably is a message from the
  239. run-time library, `libg2c'.
  240.  
  241.    The message means that your program has attempted to use a file unit
  242. number that is out of the range accepted by `libg2c'.  Normally, this
  243. range is 0 through 99, and the high end of the range is controlled by a
  244. `libg2c' source-file macro named `MXUNIT'.
  245.  
  246.    If you can easily change your program to use unit numbers in the
  247. range 0 through 99, you should do so.
  248.  
  249.    Otherwise, see *Note Larger File Unit Numbers::., for information on
  250. how to change `MXUNIT' in `libg2c' so you can build and install a new
  251. version of `libg2c' that supports the larger unit numbers you need.
  252.  
  253.    *Note:* While `libg2c' places a limit on the range of Fortran
  254. file-unit numbers, the underlying library and operating system might
  255. impose different kinds of limits.  For example, some systems limit the
  256. number of files simultaneously open by a running program.  Information
  257. on how to increase these limits should be found in your system's
  258. documentation.
  259.  
  260. 
  261. File: g77.info,  Node: Floating-point precision,  Next: Inconsistent Calling Sequences,  Prev: Large File Unit Numbers,  Up: Working Programs
  262.  
  263. Floating-point precision
  264. ------------------------
  265.  
  266.    If your program depends on exact IEEE 754 floating-point handling it
  267. may help on some systems--specifically x86 or m68k hardware--to use the
  268. `-ffloat-store' option or to reset the precision flag on the
  269. floating-point unit.  *Note Optimize Options::.
  270.  
  271.    However, it might be better simply to put the FPU into double
  272. precision mode and not take the performance hit of `-ffloat-store'.  On
  273. x86 and m68k GNU systems you can do this with a technique similar to
  274. that for turning on floating-point exceptions (*note Floating-point
  275. Exception Handling::.).  The control word could be set to double
  276. precision by replacing the `__setfpucw' call with one like this:
  277.        __setfpucw ((_FPU_DEFAULT & ~_FPU_EXTENDED) | _FPU_DOUBLE);
  278.    (It is not clear whether this has any effect on the operation of the
  279. GNU maths library, but we have no evidence of it causing trouble.)
  280.  
  281.    Some targets (such as the Alpha) may need special options for full
  282. IEEE conformance.  *Note Hardware Models and Configurations:
  283. (gcc)Submodel Options.
  284.  
  285. 
  286. File: g77.info,  Node: Inconsistent Calling Sequences,  Prev: Floating-point precision,  Up: Working Programs
  287.  
  288. Inconsistent Calling Sequences
  289. ------------------------------
  290.  
  291.    Code containing inconsistent calling sequences in the same file is
  292. normally rejected--see *Note GLOBALS::..  (Use, say, `ftnchek' to ensure
  293. consistency across source files.  *Note Generating Skeletons and
  294. Prototypes with `f2c': f2c Skeletons and Prototypes.)
  295.  
  296.    Mysterious errors, which may appear to be code generation problems,
  297. can appear specifically on the x86 architecture with some such
  298. inconsistencies.  On x86 hardware, floating-point return values of
  299. functions are placed on the floating-point unit's register stack, not
  300. the normal stack.  Thus calling a `REAL' or `DOUBLE PRECISION'
  301. `FUNCTION' as some other sort of procedure, or vice versa, scrambles
  302. the floating-point stack.  This may break unrelated code executed
  303. later.  Similarly if, say, external C routines are written incorrectly.
  304.  
  305. 
  306. File: g77.info,  Node: Overly Convenient Options,  Next: Faster Programs,  Prev: Working Programs,  Up: Collected Fortran Wisdom
  307.  
  308. Overly Convenient Command-line Options
  309. ======================================
  310.  
  311.    These options should be used only as a quick-and-dirty way to
  312. determine how well your program will run under different compilation
  313. models without having to change the source.  Some are more problematic
  314. than others, depending on how portable and maintainable you want the
  315. program to be (and, of course, whether you are allowed to change it at
  316. all is crucial).
  317.  
  318.    You should not continue to use these command-line options to compile
  319. a given program, but rather should make changes to the source code:
  320.  
  321. `-finit-local-zero'
  322.      (This option specifies that any uninitialized local variables and
  323.      arrays have default initialization to binary zeros.)
  324.  
  325.      Many other compilers do this automatically, which means lots of
  326.      Fortran code developed with those compilers depends on it.
  327.  
  328.      It is safer (and probably would produce a faster program) to find
  329.      the variables and arrays that need such initialization and provide
  330.      it explicitly via `DATA', so that `-finit-local-zero' is not
  331.      needed.
  332.  
  333.      Consider using `-Wuninitialized' (which requires `-O') to find
  334.      likely candidates, but do not specify `-finit-local-zero' or
  335.      `-fno-automatic', or this technique won't work.
  336.  
  337. `-fno-automatic'
  338.      (This option specifies that all local variables and arrays are to
  339.      be treated as if they were named in `SAVE' statements.)
  340.  
  341.      Many other compilers do this automatically, which means lots of
  342.      Fortran code developed with those compilers depends on it.
  343.  
  344.      The effect of this is that all non-automatic variables and arrays
  345.      are made static, that is, not placed on the stack or in heap
  346.      storage.  This might cause a buggy program to appear to work
  347.      better.  If so, rather than relying on this command-line option
  348.      (and hoping all compilers provide the equivalent one), add `SAVE'
  349.      statements to some or all program unit sources, as appropriate.
  350.      Consider using `-Wuninitialized' (which requires `-O') to find
  351.      likely candidates, but do not specify `-finit-local-zero' or
  352.      `-fno-automatic', or this technique won't work.
  353.  
  354.      The default is `-fautomatic', which tells `g77' to try and put
  355.      variables and arrays on the stack (or in fast registers) where
  356.      possible and reasonable.  This tends to make programs faster.
  357.  
  358.      *Note:* Automatic variables and arrays are not affected by this
  359.      option.  These are variables and arrays that are *necessarily*
  360.      automatic, either due to explicit statements, or due to the way
  361.      they are declared.  Examples include local variables and arrays
  362.      not given the `SAVE' attribute in procedures declared `RECURSIVE',
  363.      and local arrays declared with non-constant bounds (automatic
  364.      arrays).  Currently, `g77' supports only automatic arrays, not
  365.      `RECURSIVE' procedures or other means of explicitly specifying
  366.      that variables or arrays are automatic.
  367.  
  368. `-fGROUP-intrinsics-hide'
  369.      Change the source code to use `EXTERNAL' for any external procedure
  370.      that might be the name of an intrinsic.  It is easy to find these
  371.      using `-fGROUP-intrinsics-disable'.
  372.  
  373. 
  374. File: g77.info,  Node: Faster Programs,  Prev: Overly Convenient Options,  Up: Collected Fortran Wisdom
  375.  
  376. Faster Programs
  377. ===============
  378.  
  379.    Aside from the usual `gcc' options, such as `-O', `-ffast-math', and
  380. so on, consider trying some of the following approaches to speed up
  381. your program (once you get it working).
  382.  
  383. * Menu:
  384.  
  385. * Aligned Data::
  386. * Prefer Automatic Uninitialized Variables::
  387. * Avoid f2c Compatibility::
  388. * Use Submodel Options::
  389.  
  390. 
  391. File: g77.info,  Node: Aligned Data,  Next: Prefer Automatic Uninitialized Variables,  Up: Faster Programs
  392.  
  393. Aligned Data
  394. ------------
  395.  
  396.    On some systems, such as those with Pentium Pro CPUs, programs that
  397. make heavy use of `REAL(KIND=2)' (`DOUBLE PRECISION') might run much
  398. slower than possible due to the compiler not aligning these 64-bit
  399. values to 64-bit boundaries in memory.  (The effect also is present,
  400. though to a lesser extent, on the 586 (Pentium) architecture.)
  401.  
  402.    The Intel x86 architecture generally ensures that these programs will
  403. work on all its implementations, but particular implementations (such
  404. as Pentium Pro) perform better with more strict alignment.  (Such
  405. behavior isn't unique to the Intel x86 architecture.)  Other
  406. architectures might *demand* 64-bit alignment of 64-bit data.
  407.  
  408.    There are a variety of approaches to use to address this problem:
  409.  
  410.    * Order your `COMMON' and `EQUIVALENCE' areas such that the
  411.      variables and arrays with the widest alignment guidelines come
  412.      first.
  413.  
  414.      For example, on most systems, this would mean placing
  415.      `COMPLEX(KIND=2)', `REAL(KIND=2)', and `INTEGER(KIND=2)' entities
  416.      first, followed by `REAL(KIND=1)', `INTEGER(KIND=1)', and
  417.      `LOGICAL(KIND=1)' entities, then `INTEGER(KIND=6)' entities, and
  418.      finally `CHARACTER' and `INTEGER(KIND=3)' entities.
  419.  
  420.      The reason to use such placement is it makes it more likely that
  421.      your data will be aligned properly, without requiring you to do
  422.      detailed analysis of each aggregate (`COMMON' and `EQUIVALENCE')
  423.      area.
  424.  
  425.      Specifically, on systems where the above guidelines are
  426.      appropriate, placing `CHARACTER' entities before `REAL(KIND=2)'
  427.      entities can work just as well, but only if the number of bytes
  428.      occupied by the `CHARACTER' entities is divisible by the
  429.      recommended alignment for `REAL(KIND=2)'.
  430.  
  431.      By ordering the placement of entities in aggregate areas according
  432.      to the simple guidelines above, you avoid having to carefully
  433.      count the number of bytes occupied by each entity to determine
  434.      whether the actual alignment of each subsequent entity meets the
  435.      alignment guidelines for the type of that entity.
  436.  
  437.      If you don't ensure correct alignment of `COMMON' elements, the
  438.      compiler may be forced by some systems to violate the Fortran
  439.      semantics by adding padding to get `DOUBLE PRECISION' data
  440.      properly aligned.  If the unfortunate practice is employed of
  441.      overlaying different types of data in the `COMMON' block, the
  442.      different variants of this block may become misaligned with
  443.      respect to each other.  Even if your platform doesn't require
  444.      strict alignment, `COMMON' should be laid out as above for
  445.      portability.  (Unfortunately the FORTRAN 77 standard didn't
  446.      anticipate this possible requirement, which is
  447.      compiler-independent on a given platform.)
  448.  
  449.    * Use the (x86-specific) `-malign-double' option when compiling
  450.      programs for the Pentium and Pentium Pro architectures (called 586
  451.      and 686 in the `gcc' configuration subsystem).  The warning about
  452.      this in the `gcc' manual isn't generally relevant to Fortran, but
  453.      using it will force `COMMON' to be padded if necessary to align
  454.      `DOUBLE PRECISION' data.
  455.  
  456.      When `DOUBLE PRECISION' data is forcibly aligned in `COMMON' by
  457.      `g77' due to specifying `-malign-double', `g77' issues a warning
  458.      about the need to insert padding.
  459.  
  460.      In this case, each and every program unit that uses the same
  461.      `COMMON' area must specify the same layout of variables and their
  462.      types for that area and be compiled with `-malign-double' as well.
  463.      `g77' will issue warnings in each case, but as long as every
  464.      program unit using that area is compiled with the same warnings,
  465.      the resulting object files should work when linked together unless
  466.      the program makes additional assumptions about `COMMON' area
  467.      layouts that are outside the scope of the FORTRAN 77 standard, or
  468.      uses `EQUIVALENCE' or different layouts in ways that assume no
  469.      padding is ever inserted by the compiler.
  470.  
  471.    * Ensure that `crt0.o' or `crt1.o' on your system guarantees a 64-bit
  472.      aligned stack for `main()'.  The recent one from GNU (`glibc2')
  473.      will do this on x86 systems, but we don't know of any other x86
  474.      setups where it will be right.  Read your system's documentation
  475.      to determine if it is appropriate to upgrade to a more recent
  476.      version to obtain the optimal alignment.
  477.  
  478.    Progress is being made on making this work "out of the box" on
  479. future versions of `g77', `gcc', and some of the relevant operating
  480. systems (such as GNU/Linux).
  481.  
  482.    A package that tests the degree to which a Fortran compiler (such as
  483. `g77') aligns 64-bit floating-point variables and arrays is available
  484. at `ftp://alpha.gnu.org/gnu/g77/align/'.
  485.  
  486. 
  487. File: g77.info,  Node: Prefer Automatic Uninitialized Variables,  Next: Avoid f2c Compatibility,  Prev: Aligned Data,  Up: Faster Programs
  488.  
  489. Prefer Automatic Uninitialized Variables
  490. ----------------------------------------
  491.  
  492.    If you're using `-fno-automatic' already, you probably should change
  493. your code to allow compilation with `-fautomatic' (the default), to
  494. allow the program to run faster.
  495.  
  496.    Similarly, you should be able to use `-fno-init-local-zero' (the
  497. default) instead of `-finit-local-zero'.  This is because it is rare
  498. that every variable affected by these options in a given program
  499. actually needs to be so affected.
  500.  
  501.    For example, `-fno-automatic', which effectively `SAVE's every local
  502. non-automatic variable and array, affects even things like `DO'
  503. iteration variables, which rarely need to be `SAVE'd, and this often
  504. reduces run-time performances.  Similarly, `-fno-init-local-zero'
  505. forces such variables to be initialized to zero--when `SAVE'd (such as
  506. when `-fno-automatic'), this by itself generally affects only startup
  507. time for a program, but when not `SAVE'd, it can slow down the
  508. procedure every time it is called.
  509.  
  510.    *Note Overly Convenient Command-Line Options: Overly Convenient
  511. Options, for information on the `-fno-automatic' and
  512. `-finit-local-zero' options and how to convert their use into selective
  513. changes in your own code.
  514.  
  515. 
  516. File: g77.info,  Node: Avoid f2c Compatibility,  Next: Use Submodel Options,  Prev: Prefer Automatic Uninitialized Variables,  Up: Faster Programs
  517.  
  518. Avoid f2c Compatibility
  519. -----------------------
  520.  
  521.    If you aren't linking with any code compiled using `f2c', try using
  522. the `-fno-f2c' option when compiling *all* the code in your program.
  523. (Note that `libf2c' is *not* an example of code that is compiled using
  524. `f2c'--it is compiled by a C compiler, typically `gcc'.)
  525.  
  526. 
  527. File: g77.info,  Node: Use Submodel Options,  Prev: Avoid f2c Compatibility,  Up: Faster Programs
  528.  
  529. Use Submodel Options
  530. --------------------
  531.  
  532.    Using an appropriate `-m' option to generate specific code for your
  533. CPU may be worthwhile, though it may mean the executable won't run on
  534. other versions of the CPU that don't support the same instruction set.
  535. *Note Hardware Models and Configurations: (gcc)Submodel Options.  For
  536. instance on an x86 system the compiler might have been built--as shown
  537. by `g77 -v'--for the target `i386-pc-linux-gnu', i.e. an `i386' CPU.
  538. In that case to generate code best optimized for a Pentium you could
  539. use the option `-march=pentium'.
  540.  
  541.    For recent CPUs that don't have explicit support in the released
  542. version of `gcc', it *might* still be possible to get improvements with
  543. certain `-m' options.
  544.  
  545.    `-fomit-frame-pointer' can help performance on x86 systems and
  546. others.  It will, however, inhibit debugging on the systems on which it
  547. is not turned on anyway by `-O'.
  548.  
  549. 
  550. File: g77.info,  Node: Trouble,  Next: Open Questions,  Prev: Collected Fortran Wisdom,  Up: Top
  551.  
  552. Known Causes of Trouble with GNU Fortran
  553. ****************************************
  554.  
  555.    This section describes known problems that affect users of GNU
  556. Fortran.  Most of these are not GNU Fortran bugs per se--if they were,
  557. we would fix them.  But the result for a user might be like the result
  558. of a bug.
  559.  
  560.    Some of these problems are due to bugs in other software, some are
  561. missing features that are too much work to add, and some are places
  562. where people's opinions differ as to what is best.
  563.  
  564.    Information on bugs that show up when configuring, porting, building,
  565. or installing `g77' is not provided here.  *Note Problems Installing::.
  566.  
  567.    To find out about major bugs discovered in the current release and
  568. possible workarounds for them, see `ftp://alpha.gnu.org/g77.plan'.
  569.  
  570.    (Note that some of this portion of the manual is lifted directly
  571. from the `gcc' manual, with minor modifications to tailor it to users
  572. of `g77'.  Anytime a bug seems to have more to do with the `gcc'
  573. portion of `g77', see *Note Known Causes of Trouble with GNU CC:
  574. (gcc)Trouble..)
  575.  
  576. * Menu:
  577.  
  578. * But-bugs::         Bugs really in other programs or elsewhere.
  579. * Known Bugs::       Bugs known to be in this version of `g77'.
  580. * Missing Features:: Features we already know we want to add later.
  581. * Disappointments::  Regrettable things we can't change.
  582. * Non-bugs::         Things we think are right, but some others disagree.
  583. * Warnings and Errors::  Which problems in your code get warnings,
  584.                         and which get errors.
  585.  
  586. 
  587. File: g77.info,  Node: But-bugs,  Next: Known Bugs,  Up: Trouble
  588.  
  589. Bugs Not In GNU Fortran
  590. =======================
  591.  
  592.    These are bugs to which the maintainers often have to reply, "but
  593. that isn't a bug in `g77'...".  Some of these already are fixed in new
  594. versions of other software; some still need to be fixed; some are
  595. problems with how `g77' is installed or is being used; some are the
  596. result of bad hardware that causes software to misbehave in sometimes
  597. bizarre ways; some just cannot be addressed at this time until more is
  598. known about the problem.
  599.  
  600.    Please don't re-report these bugs to the `g77' maintainers--if you
  601. must remind someone how important it is to you that the problem be
  602. fixed, talk to the people responsible for the other products identified
  603. below, but preferably only after you've tried the latest versions of
  604. those products.  The `g77' maintainers have their hands full working on
  605. just fixing and improving `g77', without serving as a clearinghouse for
  606. all bugs that happen to affect `g77' users.
  607.  
  608.    *Note Collected Fortran Wisdom::, for information on behavior of
  609. Fortran programs, and the programs that compile them, that might be
  610. *thought* to indicate bugs.
  611.  
  612. * Menu:
  613.  
  614. * Signal 11 and Friends::  Strange behavior by any software.
  615. * Cannot Link Fortran Programs::  Unresolved references.
  616. * Large Common Blocks::    Problems on older GNU/Linux systems.
  617. * Debugger Problems::      When the debugger crashes.
  618. * NeXTStep Problems::      Misbehaving executables.
  619. * Stack Overflow::         More misbehaving executables.
  620. * Nothing Happens::        Less behaving executables.
  621. * Strange Behavior at Run Time::  Executables misbehaving due to
  622.                             bugs in your program.
  623. * Floating-point Errors::  The results look wrong, but....
  624.  
  625. 
  626. File: g77.info,  Node: Signal 11 and Friends,  Next: Cannot Link Fortran Programs,  Up: But-bugs
  627.  
  628. Signal 11 and Friends
  629. ---------------------
  630.  
  631.    A whole variety of strange behaviors can occur when the software, or
  632. the way you are using the software, stresses the hardware in a way that
  633. triggers hardware bugs.  This might seem hard to believe, but it
  634. happens frequently enough that there exist documents explaining in
  635. detail what the various causes of the problems are, what typical
  636. symptoms look like, and so on.
  637.  
  638.    Generally these problems are referred to in this document as "signal
  639. 11" crashes, because the Linux kernel, running on the most popular
  640. hardware (the Intel x86 line), often stresses the hardware more than
  641. other popular operating systems.  When hardware problems do occur under
  642. GNU/Linux on x86 systems, these often manifest themselves as "signal 11"
  643. problems, as illustrated by the following diagnostic:
  644.  
  645.      sh# g77 myprog.f
  646.      gcc: Internal compiler error: program f771 got fatal signal 11
  647.      sh#
  648.  
  649.    It is *very* important to remember that the above message is *not*
  650. the only one that indicates a hardware problem, nor does it always
  651. indicate a hardware problem.
  652.  
  653.    In particular, on systems other than those running the Linux kernel,
  654. the message might appear somewhat or very different, as it will if the
  655. error manifests itself while running a program other than the `g77'
  656. compiler.  For example, it will appear somewhat different when running
  657. your program, when running Emacs, and so on.
  658.  
  659.    How to cope with such problems is well beyond the scope of this
  660. manual.
  661.  
  662.    However, users of Linux-based systems (such as GNU/Linux) should
  663. review `http://www.bitwizard.nl/sig11/', a source of detailed
  664. information on diagnosing hardware problems, by recognizing their
  665. common symptoms.
  666.  
  667.    Users of other operating systems and hardware might find this
  668. reference useful as well.  If you know of similar material for another
  669. hardware/software combination, please let us know so we can consider
  670. including a reference to it in future versions of this manual.
  671.  
  672. 
  673. File: g77.info,  Node: Cannot Link Fortran Programs,  Next: Large Common Blocks,  Prev: Signal 11 and Friends,  Up: But-bugs
  674.  
  675. Cannot Link Fortran Programs
  676. ----------------------------
  677.  
  678.    On some systems, perhaps just those with out-of-date (shared?)
  679. libraries, unresolved-reference errors happen when linking
  680. `g77'-compiled programs (which should be done using `g77').
  681.  
  682.    If this happens to you, try appending `-lc' to the command you use
  683. to link the program, e.g. `g77 foo.f -lc'.  `g77' already specifies
  684. `-lg2c -lm' when it calls the linker, but it cannot also specify `-lc'
  685. because not all systems have a file named `libc.a'.
  686.  
  687.    It is unclear at this point whether there are legitimately installed
  688. systems where `-lg2c -lm' is insufficient to resolve code produced by
  689. `g77'.
  690.  
  691.    If your program doesn't link due to unresolved references to names
  692. like `_main', make sure you're using the `g77' command to do the link,
  693. since this command ensures that the necessary libraries are loaded by
  694. specifying `-lg2c -lm' when it invokes the `gcc' command to do the
  695. actual link.  (Use the `-v' option to discover more about what actually
  696. happens when you use the `g77' and `gcc' commands.)
  697.  
  698.    Also, try specifying `-lc' as the last item on the `g77' command
  699. line, in case that helps.
  700.  
  701. 
  702. File: g77.info,  Node: Large Common Blocks,  Next: Debugger Problems,  Prev: Cannot Link Fortran Programs,  Up: But-bugs
  703.  
  704. Large Common Blocks
  705. -------------------
  706.  
  707.    On some older GNU/Linux systems, programs with common blocks larger
  708. than 16MB cannot be linked without some kind of error message being
  709. produced.
  710.  
  711.    This is a bug in older versions of `ld', fixed in more recent
  712. versions of `binutils', such as version 2.6.
  713.  
  714. 
  715. File: g77.info,  Node: Debugger Problems,  Next: NeXTStep Problems,  Prev: Large Common Blocks,  Up: But-bugs
  716.  
  717. Debugger Problems
  718. -----------------
  719.  
  720.    There are some known problems when using `gdb' on code compiled by
  721. `g77'.  Inadequate investigation as of the release of 0.5.16 results in
  722. not knowing which products are the culprit, but `gdb-4.14' definitely
  723. crashes when, for example, an attempt is made to print the contents of
  724. a `COMPLEX(KIND=2)' dummy array, on at least some GNU/Linux machines,
  725. plus some others.  Attempts to access assumed-size arrays are also
  726. known to crash recent versions of `gdb'.  (`gdb''s Fortran support was
  727. done for a different compiler and isn't properly compatible with `g77'.)
  728.  
  729. 
  730. File: g77.info,  Node: NeXTStep Problems,  Next: Stack Overflow,  Prev: Debugger Problems,  Up: But-bugs
  731.  
  732. NeXTStep Problems
  733. -----------------
  734.  
  735.    Developers of Fortran code on NeXTStep (all architectures) have to
  736. watch out for the following problem when writing programs with large,
  737. statically allocated (i.e. non-stack based) data structures (common
  738. blocks, saved arrays).
  739.  
  740.    Due to the way the native loader (`/bin/ld') lays out data
  741. structures in virtual memory, it is very easy to create an executable
  742. wherein the `__DATA' segment overlaps (has addresses in common) with
  743. the `UNIX STACK' segment.
  744.  
  745.    This leads to all sorts of trouble, from the executable simply not
  746. executing, to bus errors.  The NeXTStep command line tool `ebadexec'
  747. points to the problem as follows:
  748.  
  749.      % /bin/ebadexec a.out
  750.      /bin/ebadexec: __LINKEDIT segment (truncated address = 0x3de000
  751.      rounded size = 0x2a000) of executable file: a.out overlaps with UNIX
  752.      STACK segment (truncated address = 0x400000 rounded size =
  753.      0x3c00000) of executable file: a.out
  754.  
  755.    (In the above case, it is the `__LINKEDIT' segment that overlaps the
  756. stack segment.)
  757.  
  758.    This can be cured by assigning the `__DATA' segment (virtual)
  759. addresses beyond the stack segment.  A conservative estimate for this
  760. is from address 6000000 (hexadecimal) onwards--this has always worked
  761. for me [Toon Moene]:
  762.  
  763.      % g77 -segaddr __DATA 6000000 test.f
  764.      % ebadexec a.out
  765.      ebadexec: file: a.out appears to be executable
  766.      %
  767.  
  768.    Browsing through `egcs/gcc/f/Makefile.in', you will find that the
  769. `f771' program itself also has to be linked with these flags--it has
  770. large statically allocated data structures.  (Version 0.5.18 reduces
  771. this somewhat, but probably not enough.)
  772.  
  773.    (The above item was contributed by Toon Moene
  774. (<toon@moene.indiv.nluug.nl>).)
  775.  
  776. 
  777. File: g77.info,  Node: Stack Overflow,  Next: Nothing Happens,  Prev: NeXTStep Problems,  Up: But-bugs
  778.  
  779. Stack Overflow
  780. --------------
  781.  
  782.    `g77' code might fail at runtime (probably with a "segmentation
  783. violation") due to overflowing the stack.  This happens most often on
  784. systems with an environment that provides substantially more heap space
  785. (for use when arbitrarily allocating and freeing memory) than stack
  786. space.
  787.  
  788.    Often this can be cured by increasing or removing your shell's limit
  789. on stack usage, typically using `limit stacksize' (in `csh' and
  790. derivatives) or `ulimit -s' (in `sh' and derivatives).
  791.  
  792.    Increasing the allowed stack size might, however, require changing
  793. some operating system or system configuration parameters.
  794.  
  795.    You might be able to work around the problem by compiling with the
  796. `-fno-automatic' option to reduce stack usage, probably at the expense
  797. of speed.
  798.  
  799.    *Note Maximum Stackable Size::, for information on patching `g77' to
  800. use different criteria for placing local non-automatic variables and
  801. arrays on the stack.
  802.  
  803.    However, if your program uses large automatic arrays (for example,
  804. has declarations like `REAL A(N)' where `A' is a local array and `N' is
  805. a dummy or `COMMON' variable that can have a large value), neither use
  806. of `-fno-automatic', nor changing the cut-off point for `g77' for using
  807. the stack, will solve the problem by changing the placement of these
  808. large arrays, as they are *necessarily* automatic.
  809.  
  810.    `g77' currently provides no means to specify that automatic arrays
  811. are to be allocated on the heap instead of the stack.  So, other than
  812. increasing the stack size, your best bet is to change your source code
  813. to avoid large automatic arrays.  Methods for doing this currently are
  814. outside the scope of this document.
  815.  
  816.    (*Note:* If your system puts stack and heap space in the same memory
  817. area, such that they are effectively combined, then a stack overflow
  818. probably indicates a program that is either simply too large for the
  819. system, or buggy.)
  820.  
  821. 
  822. File: g77.info,  Node: Nothing Happens,  Next: Strange Behavior at Run Time,  Prev: Stack Overflow,  Up: But-bugs
  823.  
  824. Nothing Happens
  825. ---------------
  826.  
  827.    It is occasionally reported that a "simple" program, such as a
  828. "Hello, World!" program, does nothing when it is run, even though the
  829. compiler reported no errors, despite the program containing nothing
  830. other than a simple `PRINT' statement.
  831.  
  832.    This most often happens because the program has been compiled and
  833. linked on a UNIX system and named `test', though other names can lead
  834. to similarly unexpected run-time behavior on various systems.
  835.  
  836.    Essentially this problem boils down to giving your program a name
  837. that is already known to the shell you are using to identify some other
  838. program, which the shell continues to execute instead of your program
  839. when you invoke it via, for example:
  840.  
  841.      sh# test
  842.      sh#
  843.  
  844.    Under UNIX and many other system, a simple command name invokes a
  845. searching mechanism that might well not choose the program located in
  846. the current working directory if there is another alternative (such as
  847. the `test' command commonly installed on UNIX systems).
  848.  
  849.    The reliable way to invoke a program you just linked in the current
  850. directory under UNIX is to specify it using an explicit pathname, as in:
  851.  
  852.      sh# ./test
  853.       Hello, World!
  854.      sh#
  855.  
  856.    Users who encounter this problem should take the time to read up on
  857. how their shell searches for commands, how to set their search path,
  858. and so on.  The relevant UNIX commands to learn about include `man',
  859. `info' (on GNU systems), `setenv' (or `set' and `env'), `which', and
  860. `find'.
  861.  
  862. 
  863. File: g77.info,  Node: Strange Behavior at Run Time,  Next: Floating-point Errors,  Prev: Nothing Happens,  Up: But-bugs
  864.  
  865. Strange Behavior at Run Time
  866. ----------------------------
  867.  
  868.    `g77' code might fail at runtime with "segmentation violation", "bus
  869. error", or even something as subtle as a procedure call overwriting a
  870. variable or array element that it is not supposed to touch.
  871.  
  872.    These can be symptoms of a wide variety of actual bugs that occurred
  873. earlier during the program's run, but manifested themselves as
  874. *visible* problems some time later.
  875.  
  876.    Overflowing the bounds of an array--usually by writing beyond the
  877. end of it--is one of two kinds of bug that often occurs in Fortran code.
  878. (Compile your code with the `-fbounds-check' option to catch many of
  879. these kinds of errors at program run time.)
  880.  
  881.    The other kind of bug is a mismatch between the actual arguments
  882. passed to a procedure and the dummy arguments as declared by that
  883. procedure.
  884.  
  885.    Both of these kinds of bugs, and some others as well, can be
  886. difficult to track down, because the bug can change its behavior, or
  887. even appear to not occur, when using a debugger.
  888.  
  889.    That is, these bugs can be quite sensitive to data, including data
  890. representing the placement of other data in memory (that is, pointers,
  891. such as the placement of stack frames in memory).
  892.  
  893.    `g77' now offers the ability to catch and report some of these
  894. problems at compile, link, or run time, such as by generating code to
  895. detect references to beyond the bounds of most arrays (except
  896. assumed-size arrays), and checking for agreement between calling and
  897. called procedures.  Future improvements are likely to be made in the
  898. procedure-mismatch area, at least.
  899.  
  900.    In the meantime, finding and fixing the programming bugs that lead
  901. to these behaviors is, ultimately, the user's responsibility, as
  902. difficult as that task can sometimes be.
  903.  
  904.    One runtime problem that has been observed might have a simple
  905. solution.  If a formatted `WRITE' produces an endless stream of spaces,
  906. check that your program is linked against the correct version of the C
  907. library.  The configuration process takes care to account for your
  908. system's normal `libc' not being ANSI-standard, which will otherwise
  909. cause this behaviour.  If your system's default library is
  910. ANSI-standard and you subsequently link against a non-ANSI one, there
  911. might be problems such as this one.
  912.  
  913.    Specifically, on Solaris2 systems, avoid picking up the `BSD'
  914. library from `/usr/ucblib'.
  915.  
  916. 
  917. File: g77.info,  Node: Floating-point Errors,  Prev: Strange Behavior at Run Time,  Up: But-bugs
  918.  
  919. Floating-point Errors
  920. ---------------------
  921.  
  922.    Some programs appear to produce inconsistent floating-point results
  923. compiled by `g77' versus by other compilers.
  924.  
  925.    Often the reason for this behavior is the fact that floating-point
  926. values are represented on almost all Fortran systems by
  927. *approximations*, and these approximations are inexact even for
  928. apparently simple values like 0.1, 0.2, 0.3, 0.4, 0.6, 0.7, 0.8, 0.9,
  929. 1.1, and so on.  Most Fortran systems, including all current ports of
  930. `g77', use binary arithmetic to represent these approximations.
  931.  
  932.    Therefore, the exact value of any floating-point approximation as
  933. manipulated by `g77'-compiled code is representable by adding some
  934. combination of the values 1.0, 0.5, 0.25, 0.125, and so on (just keep
  935. dividing by two) through the precision of the fraction (typically
  936. around 23 bits for `REAL(KIND=1)', 52 for `REAL(KIND=2)'), then
  937. multiplying the sum by a integral power of two (in Fortran, by `2**N')
  938. that typically is between -127 and +128 for `REAL(KIND=1)' and -1023
  939. and +1024 for `REAL(KIND=2)', then multiplying by -1 if the number is
  940. negative.
  941.  
  942.    So, a value like 0.2 is exactly represented in decimal--since it is
  943. a fraction, `2/10', with a denominator that is compatible with the base
  944. of the number system (base 10).  However, `2/10' cannot be represented
  945. by any finite number of sums of any of 1.0, 0.5, 0.25, and so on, so
  946. 0.2 cannot be exactly represented in binary notation.
  947.  
  948.    (On the other hand, decimal notation can represent any binary number
  949. in a finite number of digits.  Decimal notation cannot do so with
  950. ternary, or base-3, notation, which would represent floating-point
  951. numbers as sums of any of `1/1', `1/3', `1/9', and so on.  After all,
  952. no finite number of decimal digits can exactly represent `1/3'.
  953. Fortunately, few systems use ternary notation.)
  954.  
  955.    Moreover, differences in the way run-time I/O libraries convert
  956. between these approximations and the decimal representation often used
  957. by programmers and the programs they write can result in apparent
  958. differences between results that do not actually exist, or exist to
  959. such a small degree that they usually are not worth worrying about.
  960.  
  961.    For example, consider the following program:
  962.  
  963.      PRINT *, 0.2
  964.      END
  965.  
  966.    When compiled by `g77', the above program might output `0.20000003',
  967. while another compiler might produce a executable that outputs `0.2'.
  968.  
  969.    This particular difference is due to the fact that, currently,
  970. conversion of floating-point values by the `libg2c' library, used by
  971. `g77', handles only double-precision values.
  972.  
  973.    Since `0.2' in the program is a single-precision value, it is
  974. converted to double precision (still in binary notation) before being
  975. converted back to decimal.  The conversion to binary appends *binary*
  976. zero digits to the original value--which, again, is an inexact
  977. approximation of 0.2--resulting in an approximation that is much less
  978. exact than is connoted by the use of double precision.
  979.  
  980.    (The appending of binary zero digits has essentially the same effect
  981. as taking a particular decimal approximation of `1/3', such as
  982. `0.3333333', and appending decimal zeros to it, producing
  983. `0.33333330000000000'.  Treating the resulting decimal approximation as
  984. if it really had 18 or so digits of valid precision would make it seem
  985. a very poor approximation of `1/3'.)
  986.  
  987.    As a result of converting the single-precision approximation to
  988. double precision by appending binary zeros, the conversion of the
  989. resulting double-precision value to decimal produces what looks like an
  990. incorrect result, when in fact the result is *inexact*, and is probably
  991. no less inaccurate or imprecise an approximation of 0.2 than is
  992. produced by other compilers that happen to output the converted value
  993. as "exactly" `0.2'.  (Some compilers behave in a way that can make them
  994. appear to retain more accuracy across a conversion of a single-precision
  995. constant to double precision.  *Note Context-Sensitive Constants::, to
  996. see why this practice is illusory and even dangerous.)
  997.  
  998.    Note that a more exact approximation of the constant is computed
  999. when the program is changed to specify a double-precision constant:
  1000.  
  1001.      PRINT *, 0.2D0
  1002.      END
  1003.  
  1004.    Future versions of `g77' and/or `libg2c' might convert
  1005. single-precision values directly to decimal, instead of converting them
  1006. to double precision first.  This would tend to result in output that is
  1007. more consistent with that produced by some other Fortran
  1008. implementations.
  1009.  
  1010.    A useful source of information on floating-point computation is David
  1011. Goldberg, `What Every Computer Scientist Should Know About
  1012. Floating-Point Arithmetic', Computing Surveys, 23, March 1991, pp.
  1013. 5-48.  An online version is available at `http://docs.sun.com/', and
  1014. there is a supplemented version, in PostScript form, at
  1015. `http://www.validgh.com/goldberg/paper.ps'.
  1016.  
  1017.    Information related to the IEEE 754 floating-point standard by a
  1018. leading light can be found at
  1019. `http://http.cs.berkeley.edu/%7Ewkahan/ieee754status/'; see also slides
  1020. from the short course referenced from
  1021. `http://http.cs.berkeley.edu/%7Efateman/'.
  1022. `http://www.linuxsupportline.com/%7Ebillm/' has a brief guide to IEEE
  1023. 754, a somewhat x86-GNU/Linux-specific FAQ, and library code for
  1024. GNU/Linux x86 systems.
  1025.  
  1026.    The supplement to the PostScript-formatted Goldberg document,
  1027. referenced above, is available in HTML format.  See `Differences Among
  1028. IEEE 754 Implementations' by Doug Priest, available online at
  1029. `http://www.validgh.com/goldberg/addendum.html'.  This document
  1030. explores some of the issues surrounding computing of extended (80-bit)
  1031. results on processors such as the x86, especially when those results
  1032. are arbitrarily truncated to 32-bit or 64-bit values by the compiler as
  1033. "spills".
  1034.  
  1035.    (*Note:* `g77' specifically, and `gcc' generally, does arbitrarily
  1036. truncate 80-bit results during spills as of this writing.  It is not
  1037. yet clear whether a future version of the GNU compiler suite will offer
  1038. 80-bit spills as an option, or perhaps even as the default behavior.)
  1039.  
  1040.    The GNU C library provides routines for controlling the FPU, and
  1041. other documentation about this.
  1042.  
  1043.    *Note Floating-point precision::, regarding IEEE 754 conformance.
  1044.  
  1045.